﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NRemedy;
using BMC.ARSystem;
using System.Reflection;

namespace NRemedy.Test
{
    [TestClass]
    public class ARProxyTest
    {

        public void CreateEntryTestHelper<T>()
        {
            string ARServer = ConfigurationManager.AppSettings["ARServer"];
            string ARUid = ConfigurationManager.AppSettings["ARUid"];
            string ARPwd = ConfigurationManager.AppSettings["ARPwd"];
            NRemedyTestContext dbContext = new NRemedyTestContext();
            ARLoginContext context = new ARLoginContext(ARServer, ARUid, ARPwd);
            GlobalSettings.SetCharacterEncoding("UTF8");
            context.Login();
            ARProxy<RegularForm> proxy = new ARProxy<RegularForm>(context);
            RegularForm rf = new RegularForm();
            string submitter = DateTime.Now.ToString() + "Jibin Wang";
            string assign = DateTime.Now.ToString();
            string shortDiscription = DateTime.Now.ToString();

            rf.Submitter = submitter;
            rf.Assigned_To = assign;
            rf.Short_Description = shortDiscription;
            string entryID = proxy.CreateEntry(rf);

            Assert.AreEqual(assign, dbContext.RegularFormDataBases.Where(o => o.Request_ID == entryID).FirstOrDefault().Assigned_To);
            Assert.AreEqual(shortDiscription, dbContext.RegularFormDataBases.Where(o => o.Request_ID == entryID).FirstOrDefault().Short_Description);
        }

        [TestMethod]
        public void CreateEntryTest()
        {
            CreateEntryTestHelper<RegularForm>();
        }

        public void DeleteEntryTestHelper<T>()
        {
            string ARServer = ConfigurationManager.AppSettings["ARServer"];
            string ARUid = ConfigurationManager.AppSettings["ARUid"];
            string ARPwd = ConfigurationManager.AppSettings["ARPwd"];
            NRemedyTestContext dbContext = new NRemedyTestContext();
            ARLoginContext context = new ARLoginContext(ARServer, ARUid, ARPwd);
            context.Login();
            ARProxy<RegularForm> proxy = new ARProxy<RegularForm>(context);
            RegularForm rf = new RegularForm();
            string submitter = DateTime.Now.ToString() + "Jibin Wang";
            string assign = DateTime.Now.ToString();
            string shortDiscription = DateTime.Now.ToString();

            rf.Submitter = submitter;
            rf.Assigned_To = assign;
            rf.Short_Description = shortDiscription;
            string entryID = proxy.CreateEntry(rf);
            RegularForm rfDelete = proxy.GetEntry(entryID);
            proxy.DeleteEntry(rfDelete);
            Assert.AreEqual(0, dbContext.RegularFormDataBases.Where(o => o.Request_ID == entryID).Count());
        }

        [TestMethod]
        public void DeleteEntryTest()
        {
            DeleteEntryTestHelper<RegularForm>();
        }

        public void GetEntryTestHelper<T>()
        {
            string ARServer = ConfigurationManager.AppSettings["ARServer"];
            string ARUid = ConfigurationManager.AppSettings["ARUid"];
            string ARPwd = ConfigurationManager.AppSettings["ARPwd"];
            NRemedyTestContext dbContext = new NRemedyTestContext();
            ARLoginContext context = new ARLoginContext(ARServer, ARUid, ARPwd);
            IARServer server = new ARServer();
            context.Login();
            ARProxy<RegularForm> proxy = new ARProxy<RegularForm>(context);
            RegularForm rf = new RegularForm();
            string submitter = DateTime.Now.ToString() + "Jibin Wang";
            string assign = DateTime.Now.ToString();
            string shortDiscription = DateTime.Now.ToString();

            rf.Submitter = submitter;
            rf.Assigned_To = assign;
            rf.Short_Description = shortDiscription;
            string entryID = proxy.CreateEntry(rf);

            RegularForm rfAR = proxy.GetEntry(entryID);
            RegularFormDataBase rfDB = dbContext.RegularFormDataBases.Where(o => o.Request_ID == entryID).FirstOrDefault();
            CompareTheTwoObjectSomePropertyByTheFirstObject(rfDB, rfAR);
            //Assert.AreEqual(rfAR.Submitter, rfDB.Submitter);
            //Assert.AreEqual(rfAR.Assigned_To, rfDB.Assigned_To);
            //Assert.AreEqual(rfAR.Short_Description, rfDB.Short_Description);
        }

        [TestMethod]
        public void GetEntryTest()
        {
            GetEntryTestHelper<RegularFormDataBase>();
        }

        public void SetEntryTestHelper<T>()
        {
            string ARServer = ConfigurationManager.AppSettings["ARServer"];
            string ARUid = ConfigurationManager.AppSettings["ARUid"];
            string ARPwd = ConfigurationManager.AppSettings["ARPwd"];
            NRemedyTestContext dbContext = new NRemedyTestContext();
            ARLoginContext context = new ARLoginContext(ARServer, ARUid, ARPwd);
            context.Login();
            ARProxy<RegularForm> proxy = new ARProxy<RegularForm>(context);
            RegularForm rf = new RegularForm();
            string submitter = DateTime.Now.ToString() + "Jibin Wang";
            string assign = DateTime.Now.ToString();
            string shortDiscription = DateTime.Now.ToString();

            rf.Submitter = submitter;
            rf.Assigned_To = assign;
            rf.Short_Description = shortDiscription;
            string entryID = proxy.CreateEntry(rf);
            string change = "Hello QWWWW";
            RegularForm rfAR = proxy.GetEntry(entryID);
            //rfAR.Request_ID = entryID;
            rfAR.Submitter = change;
            proxy.SetEntry(rfAR);
            RegularFormDataBase rfDB = dbContext.RegularFormDataBases.Where(o => o.Request_ID == entryID).FirstOrDefault();
            Assert.AreEqual(change, rfDB.Submitter);
        }

        [TestMethod]
        public void SetEntryTest()
        {
            SetEntryTestHelper<RegularFormDataBase>();
        }

        public void CompareTheTwoObjectSomePropertyByTheFirstObject(Object o, Object o1)
        {
            foreach (var item in o.GetType().GetProperties())
            {
                var item1 = o1.GetType().GetProperty(item.Name);
                Assert.AreEqual(item.GetValue(o, null), item1.GetValue(o1, null));
            }
        }

    }
}
